﻿/******************************************************************************
 * 【本类功能概述】                                 					      *
 *  版权所有（C）2017-20XX，匠梦科技                                         *
 *  保留所有权利。                                                            *
 ******************************************************************************
 *  作者 : <hughjk>
 *  创建时间:2019年5月19日 14:59:28
 *  文件描述:扩展代码
 *****************************************************************************/
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Debug = UnityEngine.Debug;
namespace Util
{
    public static partial class StringTool
    {
        public const char Split1 = ';';
        public const char Split2 = '|';
        public const char Split3 = '/';

        public static Color toColor(this string s)
        {
            var splitValue = s.toFloatList(Split1);
            return new Color(splitValue[0], splitValue[1], splitValue[2], splitValue[3]);
        }

        public static Color32 toColor32(this string s)
        {
            var splitValue = s.toByteList(Split1);
            return new Color32(splitValue[0], splitValue[1], splitValue[2], splitValue[3]);
        }

        public static Rect toRect(this string s)
        {
            var splitValue = s.toFloatList(Split1);
            return new Rect(splitValue[0], splitValue[1], splitValue[2], splitValue[3]);
        }

        public static Quaternion toQuaternion(this string value)
        {
            var splitValue = value.toFloatList(Split1);
            return new Quaternion(splitValue[0], splitValue[1], splitValue[2], splitValue[3]);
        }

        /// <summary>
        /// 字符串转int
        /// </summary>
        /// <param name="s">字符串</param>
        /// <returns></returns>
        public static int toInt(this string s)
        {
            int v = -1;
            if (s == null)
            {
                return -1;
            }
            try
            {
                if (s.Length > 0 && ((s[0] >= '0' && s[0] <= '9') || s[0] == '-'))
                {
                    v = int.Parse(s);
                }
                else
                {
                    sb.Remove(0, sb.Length);
                    for (int i = 0; i < s.Length; i++)
                    {
                        if ((s[i] >= '0' && s[i] <= '9') || s[i] == '-')
                        {
                            sb.Append(s[i]);
                        }
                    }
                    if (sb.Length <= 0)
                    {
                        //debug.log("转换 int 解析失败!!  ", s);
                        v = -1;
                    }
                    else
                    {
                        v = int.Parse(sb.ToString());
                    }
                }
            }
            catch (System.Exception ex)
            {
                Debug.Log("转换 int  数值出错 !!!!" + s + ex.ToString());
            }
            return v;
        }

        public static List<int> toInt(this List<float> s)
        {
            List<int> v = new List<int>();
            for (int i = 0; i < s.Count; i++)
            {
                v.Add(s[i].toInt());
            }
            return v;
        }

        public static string toXlsVector3(this Vector3 s)
        {
            return "" + s.x + Split1 + s.y + Split1 + s.z;
        }

        public static float toFloat(this string s)
        {
            float v = -1;
            try
            {
                v = float.Parse(s);
            }
            catch 
            {
                //debug.log(debug.Level.error_3, "转换 float 错误!!!!", s, ex.ToString());
            }
            return v;
        }

        public static string toString(this string s)
        {
            return s;
        }

       
        public static string[] toSplit(this string s, char gap = Split1)
        {
            string[] vs = s.Split(gap);
            if (vs == null || vs.Length <= 0)
            {
                vs = new string[1];
                vs[0] = s;
            }
            return vs;
        }

        public static List<int> toIntList(this List<string> v)
        {
            List<int> rv = new List<int>();
            for (int i = 0; i < v.Count; i++)
            {
                rv.Add(v[i].toInt());
            }
            return rv;
        }

        public static List<int> toIntList(this string[] v)
        {
            List<int> rv = new List<int>();
            for (int i = 0; i < v.Length; i++)
            {
                rv.Add(v[i].toInt());
            }
            return rv;
        }
        /// <summary>
        /// 字符串转intList
        /// </summary>
        public static int[] toIntArr(this string s, char gap = Split1)
        {
            string[] satt = s.toSplit(gap);
            int[] list = new int[satt.Length];
            for (int i = 0; i < satt.Length; i++)
            {
                if (satt[i].toType() == bType.Int)
                {
                    list[i] = satt[i].toInt();
                }
            }
            return list;
        }

        public static List<float> toFloatList(this List<string> v)
        {
            List<float> rv = new List<float>();
            for (int i = 0; i < v.Count; i++)
            {
                rv.Add(v[i].toFloat());
            }
            return rv;
        }

        public static List<float> toFloatList(this string[] v)
        {
            List<float> rv = new List<float>();
            for (int i = 0; i < v.Length; i++)
            {
                rv.Add(v[i].toFloat());
            }
            return rv;
        }

        public static List<byte> toByteList(this string s, char gap = Split1)
        {
            string[] satt = s.toSplit(gap);
            byte item = 0;
            List<byte> list = new List<byte>();
            for (int i = 0; i < satt.Length; i++)
            {
                try
                {
                    item = byte.Parse(satt[i]);
                }
                catch (System.Exception ex)
                {
                    Debug.LogError(satt[i] + " \n " + ex.ToString());
                }
                list.Add(item);
            }
            return list;
        }

        public static Dictionary<int, int> toDictIntInt(this string s, char split1 = Split2, char split2 = Split1)
        {
            Dictionary<int, int> keyValues = new Dictionary<int, int>();
            if (s.isValid())
            {
                var arr = s.toSplit(split1);
                for (int i = 0; i < arr.Length; i++)
                {
                    var curv = arr[i].toIntList(split2);
                    keyValues.Add(curv[0], curv[1]);
                }
            }
            return keyValues;
        }

        public static Dictionary<int, float> toDictIntFloat(this string str, char split1 = Split2, char split2 = Split1)
        {
            Dictionary<int, float> tempdict = new Dictionary<int, float>();
            if (str.isValid())
            {
                var arr = str.toSplit(split1);
                for (int i = 0; i < arr.Length; i++)
                {
                    var curv = arr[i].toSplit(split2);
                    tempdict.Add(curv[0].toInt(), curv[1].toFloat());
                }
            }
            return tempdict;
        }

        public static Dictionary<int, string> toDictIntString(this string str, char split1 = Split2, char split2 = Split1)
        {
            Dictionary<int, string> tempdict = new Dictionary<int, string>();
            if (str.isValid())
            {
                var arr = str.toSplit(split1);
                for (int i = 0; i < arr.Length; i++)
                {
                    var curv = arr[i].toSplit(split2);
                    tempdict.Add(curv[0].toInt(), curv[1]);
                }
            }
            return tempdict;
        }

        public static Dictionary<string, int> toDictStringInt(this string str, char split1 = Split2, char split2 = Split1)
        {
            Dictionary<string, int> tempdict = new Dictionary<string, int>();
            if (str.isValid())
            {
                var arr = str.toSplit(split1);
                for (int i = 0; i < arr.Length; i++)
                {
                    var curv = arr[i].toSplit(split2);
                    tempdict.Add(curv[0], curv[1].toInt());
                }
            }
            return tempdict;
        }

        public static Dictionary<string, float> toDictStringFloat(this string str, char split1 = Split2, char split2 = Split1)
        {
            Dictionary<string, float> tempdict = new Dictionary<string, float>();
            if (str.isValid())
            {
                var arr = str.toSplit(split1);
                for (int i = 0; i < arr.Length; i++)
                {
                    var curv = arr[i].toSplit(split2);
                    tempdict.Add(curv[0], curv[1].toFloat());
                }
            }
            return tempdict;
        }

        public static Dictionary<string, string> toDictStringString(this string str, char split1 = Split2, char split2 = Split1)
        {
            Dictionary<string, string> tempdict = new Dictionary<string, string>();
            if (str.isValid())
            {
                var arr = str.toSplit(split1);
                for (int i = 0; i < arr.Length; i++)
                {
                    var curv = arr[i].toSplit(split2);
                    tempdict.Add(curv[0], curv[1]);
                }
            }
            return tempdict;
        }

        public static Vector2 toVector2(this string s, char gap = Split1)
        {
            var vs = s.toFloatList(gap);
            Vector2 vector2 = new Vector2();
            if (vs.Count == 2)
            {
                vector2.x = vs[0];
                vector2.y = vs[1];
            }
            else
            {
                //debug.log("使用toVector2 参数错误", s);
            }

            return vector2;
        }

        public static Vector3 toVector3(this string str, char split = Split1)
        {
            var strs = str.toFloatList(split);
            if (strs.Count == 3)
                return new Vector3(strs[0], strs[1], strs[2]);
            return new Vector3();
        }

        public static Vector4 toVector4(this string str, char split = Split1)
        {
            var strs = str.toFloatList(split);
            if (strs.Count == 4)
                return new Vector4(strs[0], strs[1], strs[2], strs[3]);
            return new Vector4();
        }

        public static List<Vector2> toListVector2(this string str, char split1 = Split2, char split2 = Split1)
        {
            List<Vector2> templist = new List<Vector2>();
            if (str.isValid())
            {
                var strs = str.toSplit(split1);
                for (int i = 0; i < strs.Length; i++)
                {
                    templist.Add(strs[i].toVector2(split2));
                }
            }
            return templist;
        }

        public static List<Vector3> toListVector3(this string str, char split1 = Split2, char split2 = Split1)
        {
            List<Vector3> templist = new List<Vector3>();
            if (str.isValid())
            {
                var strs = str.toSplit(split1);
                for (int i = 0; i < strs.Length; i++)
                {
                    templist.Add(strs[i].toVector3(split2));
                }
            }
            return templist;
        }

        public static List<Vector4> toListVector4(this string str, char split1 = Split2, char split2 = Split1)
        {
            List<Vector4> templist = new List<Vector4>();
            if (str.isValid())
            {
                var strs = str.toSplit(split1);
                for (int i = 0; i < strs.Length; i++)
                {
                    templist.Add(strs[i].toVector4(split2));
                }
            }
            return templist;
        }

        /// <summary>
        /// 字符串 list
        /// </summary>
        /// <param name="s"></param>
        /// <param name="gap"></param>
        /// <returns></returns>
        public static List<string> toStringList(this string s, char gap = Split1)
        {
            return new List<string>(s.toSplit(gap));
        }

        /// <summary>
        /// 字符串矩阵list row行分割符号 pos位置分割符号
        /// </summary>
        /// <param name="s"></param>
        /// <param name="row"></param>
        /// <param name="pos"></param>
        /// <returns></returns>
        public static List<List<string>> toStringMatrixList(this string s, char row = Split2, char pos = Split1)
        {
            List<List<string>> Matrix = new List<List<string>>();
            string[] rowatt = s.toSplit(row);
            for (int i = 0; i < rowatt.Length; i++)
            {
                Matrix.Add(rowatt[i].toStringList(pos));
            }
            return Matrix;
        }

        /// <summary>
        /// 字符串空间list axis1 z纬度, axis2 y纬度, axis3 x纬度
        /// </summary>
        /// <param name="s"></param>
        /// <param name="axis1"></param>
        /// <param name="axis2"></param>
        /// <param name="axis3"></param>
        /// <returns></returns>
        public static List<List<List<string>>> toStringSpaceList(this string s, char axis1 = Split3, char axis2 = Split2, char axis3 = Split1)
        {
            List<List<List<string>>> Matrix = new List<List<List<string>>>();
            string[] xs = s.toSplit(axis1);
            for (int x = 0; x < xs.Length; x++)
            {
                Matrix.Add(xs[x].toStringMatrixList());
            }
            return Matrix;
        }

        /// <summary>
        /// 字符串转intList
        /// </summary>
        public static List<int> toIntList(this string s, char gap = Split1)
        {
            string[] satt = s.toSplit(gap);
            int item = 0;
            List<int> list = new List<int>();
            for (int i = 0; i < satt.Length; i++)
            {
                try
                {
                    if (satt[i].toType() == bType.Int)
                    {
                        item = satt[i].toInt();
                        list.Add(item);
                    }
                }
                catch (System.Exception ex)
                {
                    Debug.LogError(ex.ToString());
                }
            }
            return list;
        }

        /// <summary>
        /// int矩阵 row行分割符号 pos位置分割符号
        /// </summary>
        /// <param name="s"></param>
        /// <param name="row"></param>
        /// <param name="pos"></param>
        /// <returns></returns>
        public static List<List<int>> toIntMatrixList(this string s, char row = Split2, char pos = Split1)
        {
            List<List<int>> Matrix = new List<List<int>>();
            string[] rowatt = s.toSplit(row);
            for (int j = 0; j < rowatt.Length; j++)
            {
                Matrix.Add(rowatt[j].toIntList(pos));
            }
            return Matrix;
        }

        /// <summary>
        /// Int空间list axis1 z纬度, axis2 y纬度, axis3 x纬度
        /// </summary>
        /// <param name="s"></param>
        /// <param name="axis1"></param>
        /// <param name="axis2"></param>
        /// <param name="axis3"></param>
        /// <returns></returns>
        public static List<List<List<int>>> toIntSpaceList(this string s, char axis1 = Split3, char axis2 = Split2, char axis3 = Split1)
        {
            List<List<List<int>>> Matrix = new List<List<List<int>>>();
            string[] xs = s.toSplit(axis1);
            for (int x = 0; x < xs.Length; x++)
            {
                Matrix.Add(xs[x].toIntMatrixList(axis2, axis3));
            }
            return Matrix;
        }

        /// <summary>
        /// 字符串转FloatList
        /// </summary>
        public static List<float> toFloatList(this string s, char gap = Split1)
        {
            string[] satt = s.toSplit(gap);
            float item = 0;
            List<float> list = new List<float>();
            for (int i = 0; i < satt.Length; i++)
            {
                try
                {
                    if (satt[i].toType() == bType.Float)
                    {
                        item = satt[i].toFloat();
                        list.Add(item);
                    }
                    else if (satt[i].toType() == bType.Int)
                    {
                        item = satt[i].toInt();
                        list.Add(item);
                    }
                }
                catch (System.Exception ex)
                {
                    Debug.LogError(ex.ToString());
                }

            }
            return list;
        }

        /// <summary>
        /// float矩阵 row行分割符号 pos位置分割符号
        /// </summary>
        /// <param name="s"></param>
        /// <param name="row"></param>
        /// <param name="pos"></param>
        /// <returns></returns>
        public static List<List<float>> toFloatMatrixList(this string s, char row = Split2, char pos = Split1)
        {
            List<List<float>> Matrix = new List<List<float>>();
            string[] rowatt = s.toSplit(row);
            for (int j = 0; j < rowatt.Length; j++)
            {
                Matrix.Add(rowatt[j].toFloatList(pos));
            }
            return Matrix;
        }

        /// <summary>
        /// float空间list axis1 z纬度, axis2 y纬度, axis3 x纬度
        /// </summary>
        /// <param name="s"></param>
        /// <param name="axis1"></param>
        /// <param name="axis2"></param>
        /// <param name="axis3"></param>
        /// <returns></returns>
        public static List<List<List<float>>> toFloatSpaceList(this string s, char axis1 = Split3, char axis2 = Split2, char axis3 = Split1)
        {
            List<List<List<float>>> Matrix = new List<List<List<float>>>();
            string[] xs = s.toSplit(axis1);
            for (int x = 0; x < xs.Length; x++)
            {
                Matrix.Add(xs[x].toFloatMatrixList(axis2, axis3));
            }
            return Matrix;
        }

        public static string toStringArray<T>(this List<T> list, char split1 = Split1)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                sb.Append(list[i]);
                if (i != list.Count - 1)
                {
                    sb.Append(Split1);
                }
            }
            return sb.ToString();
        }

        public static string toStringMatrix<T>(this List<List<T>> list, char row = Split2, char pos = Split1)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                var t = list[i];
                for (int j = 0; j < t.Count; j++)
                {
                    sb.Append(t);
                    if (i != t.Count - 1)
                        sb.Append(Split1);
                }
                if (i != list.Count - 1)
                    sb.Append(Split2);
            }
            return sb.ToString();
        }

        public static string toStringSpace<T>(this List<List<List<T>>> list, char axis1 = Split3, char axis2 = Split2, char axis3 = Split1)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                var t = list[i];
                for (int j = 0; j < t.Count; j++)
                {
                    var z = t[j];
                    for (int k = 0; k < z.Count; k++)
                    {
                        var c = z[k];
                        sb.Append(c);
                        if (i != z.Count - 1)
                            sb.Append(Split1);
                    }
                    if (i != t.Count - 1)
                        sb.Append(Split2);
                }
                if (i != list.Count - 1)
                    sb.Append(Split3);
            }
            return sb.ToString();
        }

        public static string toStringDic<T>(this Dictionary<int, T> dic, char row = Split2, char pos = Split1)
        {
            StringBuilder sb = new StringBuilder();
            var klist = new List<int>(dic.Keys);
            var vlist = new List<T>(dic.Values);
            int num = dic.Count;
            for (int i = 0; i < num; i++)
            {
                sb.Append(klist[i]);
                sb.Append(Split1);
                sb.Append(vlist[i]);
                if (i != num - 1)
                    sb.Append(Split2);
            }
            return sb.ToString();
        }
    }
}